<template>
	<view>
		<u-popup :show="switchRole" mode="center" :safeAreaInsetBottom="false" @close="closeSwitch"
			bgColor="transparent">
			<view class="popup1">
				<view class="close fs24" @click="closeSwitch">关闭</view>
				<view style="padding: 1px;"></view>
				<view class="fs40 fwb col1">选择身份</view>
				<view class="mt10 fs24 col89 lh28">选择您的身份，以便为您推荐更精准的内容</view>
				<!-- 显示切换到求职者选项：未登录或当前是企业身份 -->
				<template v-if="!currentUserinfo || currentUserinfo.group_id==2">
					<view class="mt70 switch_item" @click="nav(1)">
						<image src="https://admin.mosizp.cn/static/public/switch1.png" mode="aspectFill"></image>
						<view class="view flex flex-col flex-center">
							<view class="fs40 fwb">人才</view>
							<view class="mt10 fs30">我要找工作</view>
						</view>
					</view>
				</template>
				<!-- 显示切换到企业选项：未登录或当前是求职者身份 -->
				<template v-if="!currentUserinfo || currentUserinfo.group_id==1">
					<view  class="mt50 switch_item" @click="nav(2)">
						<image src="https://admin.mosizp.cn/static/public/switch2.png" mode="aspectFill"></image>
						<view class="view flex flex-col flex-center">
							<view class="fs40 fwb">用人单位</view>
							<view class="mt10 fs30">我要招人才</view>
						</view>
					</view>
				</template>
				
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: "change",
		props: {
			switchRole: {
				type: Boolean,
				default: false
			},
			userinfo: {
				type: Object,
				default: null
			}
		},
		data() {
			return {
			
			};
		},
		computed: {
			// 计算属性，获取当前用户信息
			currentUserinfo() {
				// 优先使用传入的props，如果没有则从缓存获取
				return this.userinfo || this.$core.getUserinfo();
			}
		},
		methods: {
			closeSwitch() {
				// this.switchRole = false
				this.$emit('close')
			},
			nav(groupId){
				// 使用计算属性获取用户信息
				let userinfo = this.currentUserinfo;
				
				console.log('切换身份 nav方法 - userinfo:', userinfo, 'groupId:', groupId);
				
				// 如果用户已登录，直接切换身份
				if(userinfo && userinfo.id) {
					// 显示加载提示
					uni.showLoading({
						title: '切换中...'
					});
					
					this.$core.post({
						url:'xilujob.user/switch_group',
						data:{group_id: groupId},
						success: ret=>{
							uni.hideLoading();
							console.log('切换身份成功', ret)
							this._loginSuccess(ret.data.userinfo)
							uni.showToast({
								title: '切换成功',
								icon: 'success'
							});
						},
						fail: (err) => {
							uni.hideLoading();
							uni.showToast({
								title: err.msg || '切换失败',
								icon: 'none'
							});
						}
					})
				} else {
					// 未登录时跳转到对应的登录页
					console.log('用户未登录，跳转到登录页');
					if(groupId == 2){
						uni.navigateTo({
							url: '/pages/public/login2'
						})
					}else{
						uni.navigateTo({
							url: '/pages/public/login'
						})
					}
				}
				this.$emit('close')
			},
			_loginSuccess(userinfo) {
				getApp().globalData.userinfo = userinfo;
			    this.$core.setUserinfo(userinfo);
			    this.$chatIM.connectSocket();
				
				// 触发用户登录事件，刷新页面
			    uni.$emit(getApp().globalData.Events.UserLogin, userinfo);
				
				// 判断是否需要跳转
				// 如果是求职者，或者是企业已审核通过的
			    if(userinfo.group_id==1 || (userinfo.group_model && userinfo.group_model.audit_status==1)) {
			      // 刷新当前页面以显示对应身份的内容
				  const pages = getCurrentPages();
				  if(pages.length > 0) {
					  const currentPage = pages[pages.length - 1];
					  // 如果在个人中心页面，触发刷新
					  if(currentPage.route === 'pages/public/profile') {
						  // 页面会通过 UserLogin 事件自动刷新
						  return;
					  }
				  }
				  // 其他页面返回
			      if(getCurrentPages().length > 1) {
			        uni.navigateBack({delta:1});
			      }
			    }else {
				  // 企业未审核，跳转到完善信息页面
			      uni.reLaunch({url:'/pages/company/improve_corporate_information'});
			    }
			  },
		}
	}
</script>

<style lang="scss" scoped>
	.popup1 {
		width: 600rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		position: relative;
		padding: 70rpx 65rpx;
	z-index: 999999;
		.close {
			position: absolute;
			top: 30rpx;
			right: 30rpx;
		}
	
		.switch_item {
			width: 470rpx;
			height: 180rpx;
			position: relative;
	z-index: 9999999;
			image {
				width: 470rpx;
				height: 180rpx;
				position: absolute;
				top: 0;
				left: 0;
				z-index: 1;
			}
	
			.view {
				width: 470rpx;
				height: 180rpx;
				position: absolute;
				top: 0;
				left: 0;
				z-index: 22;
				padding: 0 30rpx;
				color: #FFFFFF;
				background: transparent;
			}
		}
	}
	.lh50{height: 50rpx;line-height: 1.2;}
</style>